﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using MyWcfRestSvc.Properties;

namespace MyWcfRestSvc
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //BuildTrayIcon();
        }

        private void btnStartService_Click(object sender, RoutedEventArgs e)
        {
            FileServiceAdapter.Instance.Start(Settings.Default.DefaultServicePort);
            var info = FileServiceAdapter.Instance.GetConnectionInfo();
            string infoString = FileServiceAdapter.Instance.GetConnectionInfoString();

            StringBuilder serviceInfo = new StringBuilder(string.Format("Service name:\t{0}\nService Port:\t{1}\nHost Name:\t{2}", info.ServiceName, info.ServicePort, info.HostName));
            for (int i = 0; i < info.Addresses.Length; i++)
            {
                serviceInfo.Append(string.Format("\nIP Address {0}:\t{1}", i, info.Addresses[i]));
            }

            this.textBlock1.Text = serviceInfo.ToString();
            string codeFile = System.IO.Path.GetTempFileName();
            QRCode.Encoder(infoString, codeFile);
            this.image1.Source = new BitmapImage(new Uri(codeFile));

        }

        private void btnStopService_Click(object sender, RoutedEventArgs e)
        {
            FileServiceAdapter.Instance.Stop();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            FileServiceAdapter.Instance.Stop();
        }

        #region Tray Icon
        //Tray Icon related
        private NotifyIcon notifyIcon;
         
        private void BuildTrayIcon()
        {
            this.Visibility = System.Windows.Visibility.Collapsed;
            this.ShowInTaskbar = false;
            this.WindowState = System.Windows.WindowState.Minimized;
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

            this.notifyIcon = new NotifyIcon();
            this.notifyIcon.BalloonTipText = "Running...";
            this.notifyIcon.ShowBalloonTip(2000);
            this.notifyIcon.Text = "Running...";
            this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
            this.notifyIcon.Visible = true;
            System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
            open.Click += new EventHandler(Show);
            System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
            exit.Click += new EventHandler(Close);
            System.Windows.Forms.MenuItem hide = new System.Windows.Forms.MenuItem("Hide");
            exit.Click += new EventHandler(Hide);
            System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { open, hide, exit };
            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);

            this.notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler((o, e) =>
            {
                if (e.Button == MouseButtons.Left) this.Show(o, e);
            });
        }

        private void Show(object sender, EventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Visible;
            this.ShowInTaskbar = true;
            this.Activate();
        }

        private void Hide(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
            this.Visibility = System.Windows.Visibility.Hidden;
        }

        private void Close(object sender, EventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
        }

        #endregion Tray Icon

    }
}
